//
//  HMVisitorLoginView.swift
//  sina
//
//  Created by 崔正强 on 17/3/22.
//  Copyright © 2017年 崔正强. All rights reserved.
//

import UIKit
import SnapKit

//定义访客视图的协议 : 监听按钮的点击事件
@objc protocol HMVisitorLoginViewDelegate: NSObjectProtocol {
    //定义协议方法
    @objc optional func userWillLogin(vistorView: HMVisitorLoginView)
    @objc optional func userWillRegister(vistorView: HMVisitorLoginView)
}

class HMVisitorLoginView: UIView {

    //定义代理对象,通过点击事件让代理对象调用协议方法
    weak var delegate: HMVisitorLoginViewDelegate?
    
    //开始自定义视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //根据子页面的显示逻辑设置UI
    func updataUI(title: String, imageName: String,isHome: Bool = false) {
        //不是首页就不显示largrHouse
        self.largrHouse.isHidden = !isHome
        self.coverView.isHidden = !isHome
        self.tipLabel.text = title
        self.circleView.image = UIImage(named: imageName)
        if isHome {
            //圈圈动画
            startAnimation()
        }
    }
    
    private func startAnimation() {
        //给圈圈添加动画效果
        //1.实例化基本动画对象
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //2.设置动画效果
        animation.toValue = 2 * M_PI
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false
        //3.将动画效果添加到图层上
        circleView.layer.add(animation, forKey: nil)
    }
    
    //自定义UI
    private func setupUI() {
        //先添加子视图 再添加约束
        addSubview(circleView)
        addSubview(coverView)
        addSubview(largrHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //取消frame的布局机制
//        circleView.translatesAutoresizingMaskIntoConstraints = false
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
//            make.center.equalTo(self.snp.center)
        }
        
        largrHouse.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        coverView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(16)
            make.centerX.equalTo(circleView)
            //可以通过添加左右约束/宽度的约束来设置自动换行  不推荐
        }
        
        //在外面设置tipLabel的最大布局宽度 来设置自动换行
        tipLabel.preferredMaxLayoutWidth = 230
        
        registerBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.left.equalTo(tipLabel)
            make.width.equalTo(100)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.right.equalTo(tipLabel)
            make.width.equalTo(100)
        }
        //设置背景灰度色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
        //监听按钮的点击事件
        loginBtn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerClick), for:.touchUpInside)
    }
    
    @objc private func loginClick() {
        //通过代理对象调用协议方法
        delegate?.userWillLogin?(vistorView: self)
    }
    
    @objc private func registerClick() {
        delegate?.userWillRegister?(vistorView: self)
    }
    
    //子控件
    //懒加载子控件
    //圈圈
    private lazy var circleView: UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //大房子
    private lazy var largrHouse: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //遮罩
//    private lazy var coverView: UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    private lazy var coverView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    //提示文字
    private lazy var tipLabel : UILabel = UILabel(title: "关注一些人,回到这里关注一些人,回到这里关注一些人,回到这里关注一些人,回到这里关注一些人,回到这里", textColor:UIColor.darkGray, fontSize: 14 )
    
    //登录
    private lazy var loginBtn: UIButton = UIButton(title: "登录", textColor: UIColor.darkGray, fontSize: 15, backImageName: "common_button_white")
    
    //注册
    private lazy var registerBtn: UIButton = UIButton(title: "注册", textColor: UIColor.orange, fontSize: 15, backImageName: "common_button_white")
        
}
